package com.pzy.excel;

import com.pzy.base.exception.BizException;

import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;

public class ProductTypeConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        if (stringValue == null) {
            return null;
        }
        switch (stringValue) {
            case "电脑":
                return 1;
            case "学习用品":
                return 2;
            default:
                throw new BizException(String.format("无法识别的类型[ %s ]", stringValue));
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new WriteCellData<>(""); // Or handle as needed
        }
        switch (value) {
            case 1:
                return new WriteCellData<>("电脑");
            case 2:
                return new WriteCellData<>("学习用品");
            default:
                throw new BizException(String.format("无法识别的类型[ %s ]", value));
        }
    }
}
